package com.kaesun.ufit.tools;

import android.util.Log;



public class ExcerciseData {

	public static final int MODE_MEMBER = 0;
	public static final int MODE_TRAINER = 1;

	public String mHashCode = "";
	public String mNeoId = "";
	public String mUserId = "";
	public String mUserName = "";
	public float mWeight = 0;
	public int mCount = 0;
	public int mSet = 0;
	public float mOneRm = 0;
	public float mCalories = 0;
	public int mMode = 0;
	public int mRetCode = 0;
	public String mRetMsg = "";

	public float mMeasureWeight = 0;
	public float mMeasureDistance = 0;
	public int mMeasureCount = 0;
	public int mMeasureTime = 0;
	public int [] mMeasureSetCount = {0, 0, 0, 0, 0, 0, 0, 0};
	public int mMeasureSet = 0;
	public float mMeasureRate = 0;
	public float mMeasureCalories = 0;

	public void init() {
		mHashCode = "";
		mNeoId = "";
		mUserId = "";
		mUserName = "";
		mWeight = 0;
		mCount = 0;
		mSet = 0;
		mOneRm = 0;
		mCalories = 0;
		mMode = 0;
		mRetCode = 0;
		mRetMsg = "";

		mMeasureWeight = 0;
		mMeasureDistance = 0;
		mMeasureCount = 0;
		mMeasureTime = 0;
		for (int i=0; i<mMeasureSetCount.length; i++) {
			mMeasureSetCount[i] = 0;
		}
		mMeasureSet = 0;
		mMeasureRate = 0;
		mMeasureCalories = 0;
	}

	public int parsePacket(String packet) {
		mRetCode = -1;
		// command=w22^hashcode=1234567^neoid=UF101A1^userid=020407F54D19A603^username=홍���동^weight=0.0^count=0^set=0^onerm=0.0^calories=0.00^mode=0^retcode=1^retmsg=ok
		// command=w22^hashcode=0^neoid=UF101A1^userid=0204C3C781971203^username=트레이���02^weight=0^count=0^set=0^onerm=0^calories=0^mode=1^retcode=1^retmsg=ok

		// command = w22
		// hashcode = 고유번호
		// neoid = 장비 고유번호
		// userid = rfid tag hex 문자열(16자리)
		// username = 회원명 / 트레이너명
		// weight = 목표 운동무게
		// count = 목표 운동횟수
		// set = 목표 운동세트
		// onerm = 1RM
		// calories = 목표 소모칼로리
		// mode = 일반회원(0), 트레이너(1 : 목표값 0)
		// retcode = 실패(0), 성공(1)
		// retmsg = 결과 메시지 (오류 메시지 or ok)

		int idx = 0, start = 0, subIdx;
		String item = "", key = "", value = "";
		while (true) {
			idx = packet.indexOf('^', start);
			if (0 > idx) {
				break;
			}
			item = packet.substring(start, idx);
			//Log.e("bbb", "item = " + item);

			subIdx = item.indexOf('=');
			if (0 <= subIdx) {
				key = item.substring(0, subIdx);
				value = item.substring(subIdx + 1);
				Log.e("bbb", "key = " + key + ", value=" + value);

				if (0 < value.length()) {
					if (0 == key.compareTo("command")) {
						if (0 != value.compareTo("w22")) {
							return -2;
						}
					} else if (0 == key.compareTo("hashcode")) {
						mHashCode = value;
					} else if (0 == key.compareTo("neoid")) {
						mNeoId = value;
					} else if (0 == key.compareTo("userid")) {
						mUserId = value;
					} else if (0 == key.compareTo("username")) {
						mUserName = value;
					} else if (0 == key.compareTo("weight")) {
						mWeight = Float.parseFloat(value);
					} else if (0 == key.compareTo("count")) {
						mCount = Integer.parseInt(value, 10);
					} else if (0 == key.compareTo("set")) {
						mSet = Integer.parseInt(value, 10);
					} else if (0 == key.compareTo("onerm")) {
						mOneRm = Float.parseFloat(value);
					} else if (0 == key.compareTo("calories")) {
						mCalories = Float.parseFloat(value);
					} else if (0 == key.compareTo("mode")) {
						mMode = Integer.parseInt(value, 10);
					} else if (0 == key.compareTo("retcode")) {
						mRetCode = Integer.parseInt(value, 10);
					} else if (0 == key.compareTo("retmsg")) {
						mRetMsg = value;
					}
				}
			}

			start = idx + 1;
		}

		return mRetCode;
	}
}
